--
-- Created by IntelliJ IDEA.
-- User: galileoliu
-- Date: 2018/4/13
-- Time: 13:49
-- Copyright (c) 2018      Figure Inc.
--

-- GrayBattleTrackView

local fileName = string.getModeName(...)
local GrayBattleTrackView = class(fileName, app.mvc.ViewBase)

GrayBattleTrackView.VIEW_ROOT_RES = Res.TrackTaskGrayBattleView

TrophyIcons =
{
    [0] = "invade_icon_question.png",
    [1] = "team_trophy_icon_3.png",
    [2] = "team_trophy_icon_2.png",
    [3] = "team_trophy_icon_1.png",
}

GrayBattleSoulIcons =
{
    [1] = "snowman01.png",
    [2] = "snowman02.png",
    [3] = "snowman03.png",
    [4] = "snowman04.png",
}

GrayBattleSoulNames =
{
    [1] = "soul_red",
    [2] = "soul_blue",
    [3] = "soul_yellow",
    [4] = "soul_green",
}

function GrayBattleTrackView:init()
    self._ui["Panel/Panel_content/text"]:setString(L("ashes_task_describe"))
    -- Soul icons.
    for i = 1, 4 do
        self._ui["Panel/Panel_content/Panel_icon/head_"..i.."/Panel/head/icon"]:loadTexture(Res.npcIconPath..GrayBattleSoulIcons[i])
        self._ui["Panel/Panel_content/Panel_icon/head_"..i.."/Panel/head"]:setCascadeOpacityEnabled(true)
        self._ui["Panel/Panel_content/Panel_icon/head_"..i.."/Panel/head"]:setOpacity(153)
        self._ui["Panel/Panel_content/Panel_icon/head_"..i.."/Panel/img_own"]:setVisible(false)
        self._ui["Panel/Panel_content/Panel_icon/head_"..i.."/Panel/name"]:setString(L(GrayBattleSoulNames[i]))
    end
    local soulList = UD:getGrayBattleSoulList()
    if soulList then
        for _, soulType in ipairs(soulList) do
            if self._ui["Panel/Panel_content/Panel_icon/head_"..soulType.."/Panel/head"] ~= nil then
                self._ui["Panel/Panel_content/Panel_icon/head_"..soulType.."/Panel/head"]:setOpacity(255)
                self._ui["Panel/Panel_content/Panel_icon/head_"..soulType.."/Panel/img_own"]:setVisible(true)
            end
        end
    end

    -- Reward grade.
    local rewardGrade = UD:getGrayBattleRewardGrade()
    if rewardGrade then
        self._ui["Panel/Panel_content/icon"]:loadTexture(Res.teamUIPath..TrophyIcons[rewardGrade], 1)
    end
    
    local strRewardGet = L("ashes_has_been_ing")
    local strRewardTrophyHint = ""
    if rewardGrade == Enums.activityRewardGrade.none then
        strRewardTrophyHint = L("ashes_has_not_got_reward_yet")
    elseif rewardGrade == Enums.activityRewardGrade.bronze then
        strRewardTrophyHint = string.format(strRewardGet, L("ashes_trophy_bronze"))
    elseif rewardGrade == Enums.activityRewardGrade.silver then
        strRewardTrophyHint = string.format(strRewardGet, L("ashes_trophy_silver"))
    elseif rewardGrade == Enums.activityRewardGrade.gold then
        strRewardTrophyHint = string.format(strRewardGet, L("ashes_trophy_gold"))
    end
    self._ui["Panel/Panel_content/tlt_tips"]:setString(strRewardTrophyHint)

    -- Activity status.
    self:refreshView()
    
    self._ui["Panel/Panel_content/icon"]:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            executeFunc(self._onOperate, "showRewardTips", sender:getTouchBeganPosition())
        elseif eventType == ccui.TouchEventType.canceled or eventType == ccui.TouchEventType.ended then
            executeFunc(self._onOperate, "hideRewardTips")
        end
    end)

    self._ui["Panel/Button_ok"]:addClickEventListener(function()
        executeFunc(self._onOperate, "closeFinal")
        executeFunc(self._onCloseView)
    end)

    self._ui["Panel/Button_get"]:addClickEventListener(function()
        executeFunc(self._onOperate, "getReward")
        executeFunc(self._onCloseView)
    end)

    self._ui["Panel/Button_go"]:addClickEventListener(function()
        executeFunc(self._onOperate, "go")
        executeFunc(self._onCloseView)
        UIHandler:getSystemUi():removeAllUI()
    end)

    self._ui["Panel/Button_team"]:addClickEventListener(function()
        executeFunc(self._onOperate, "gotoTeam")
        executeFunc(self._onCloseView)
    end)

    self._ui.Button_close:addClickEventListener(function()
        executeFunc(self._onOperate, "close")
        executeFunc(self._onCloseView)
    end)

    -- 点击空白区域关闭
    self._ui.bg:setTouchEnabled(true)
    display.uiAddClick(self._ui.bg, function()
        executeFunc(self._onOperate, "close")
        executeFunc(self._onCloseView)
    end)
end

function GrayBattleTrackView:refreshView()
    if UD:isGrayBattleFinished() or not UD:hasGrayBattleInfo() then
        if rewardGrade == Enums.activityRewardGrade.none then
            self._ui["Panel/Panel_content/icon"]:loadTexture(Res.teamUIPath.."invade_icon_trophy.png", 1)
            self._ui["Panel/Button_ok"]:setVisible(true)
            self._ui["Panel/Button_get"]:setVisible(false)
        else
            self._ui["Panel/Button_ok"]:setVisible(false)
            self._ui["Panel/Button_get"]:setVisible(true)
        end
        self._ui["Panel/Button_go"]:setVisible(false)
        self._ui["Panel/Button_team"]:setVisible(false)

        local config = GD:queryEventModeById(Enums.TeamTarget.ashInvade)
        local strCondition = config.condition  -- 1;3;4
        local strRewardPoint1 = config.reward1 -- 91|0|40
        local strRewardPoint2 = config.reward2 -- 91|0|40
        local strRewardPoint3 = config.reward3 -- 91|0|40
        local temp = {}
        for w in string.gmatch(strCondition, "%d+") do
            table.insert(temp, w)
        end
        local minCount = temp[1]
        temp = {}
        for w in string.gmatch(strRewardPoint1, "%d+") do
            table.insert(temp, w)
        end
        for w in string.gmatch(strRewardPoint2, "%d+") do
            table.insert(temp, w)
        end
        for w in string.gmatch(strRewardPoint3, "%d+") do
            table.insert(temp, w)
        end
        local strRewardHint = ""
        if rewardGrade == Enums.activityRewardGrade.none then
            strRewardHint = string.format(L("ashes_fail_description"), minCount)
        elseif rewardGrade == Enums.activityRewardGrade.bronze then
            strRewardHint = string.format(L("ashes_reward_point"), temp[3])
        elseif rewardGrade == Enums.activityRewardGrade.silver then
            strRewardHint = string.format(L("ashes_reward_point"), temp[6])
        elseif rewardGrade == Enums.activityRewardGrade.gold then
            strRewardHint = string.format(L("ashes_reward_point"), temp[9])
        end
        self._ui["Panel/Panel_content/tips"]:setString(strRewardHint)
    else
        self._ui["Panel/Button_ok"]:setVisible(false)
        self._ui["Panel/Button_get"]:setVisible(false)
        self._ui["Panel/Button_go"]:setVisible(true)
        self._ui["Panel/Button_team"]:setVisible(true)

        local activityRemainTime = UD:getGrayBattleRemainTime()
        local strActivityEndHint = string.format(L("ashes_remaining_time"),TimeUtil.secondsToTime(activityRemainTime))
        self._ui["Panel/Panel_content/tips"]:setString(strActivityEndHint)

        if not self._updateAct then
            self._updateAct = self:schedule(function() self:_refreshRemainTime(self._ui["Panel/Panel_content/tips"]) end,1)
        end
    end
end

function GrayBattleTrackView:_refreshRemainTime(node)
    local activityRemainTime = UD:getGrayBattleRemainTime()
    if activityRemainTime > 0 then
        local strActivityEndHint = string.format(L("ashes_remaining_time"),TimeUtil.secondsToTime(activityRemainTime))
        node:setString(strActivityEndHint)
    else
        if self._updateAct then
            self:stopAction(self._updateAct)
            self._updateAct = nil
        end
        self:refreshView()
    end
end

return GrayBattleTrackView
